package com.ojoin.trade.t0loan_core.web.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.t0loan_core.web.dao.TbT0LoanTransanctionLogDao;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanCheckAccountInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanTransanctionLogInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanTransanctionLog;

@Repository
public class TbT0LoanTransanctionLogDaoImpl extends BaseDaoImpl<TbT0LoanTransanctionLog> implements TbT0LoanTransanctionLogDao {
	
	public SqlSession getSqlSession() {
		return this.sqlSessionTemplate;
	}

	@Override
	public List<T0LoanCheckAccountInfoVO> queryAllT0LoanCheckAccountInfoList(
			Pageable page) throws BusinessException {
		String mapMethodName = "queryAllT0LoanCheckAccountInfoList";
		try {
			return getSqlSession().selectList(getSqlName(mapMethodName),
					this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("查询T0贷款对账信息列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<T0LoanCheckAccountInfoVO> queryAllTxLoanCheckAccountInfoList(
			String operatetype, String mhtno, String startdate, String enddate, String ismhtnosplit, Pageable page) throws BusinessException {
		String mapMethodName = "queryAllTxLoanCheckAccountInfoList";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("mhtno", mhtno);
			paramMap.put("startdate", startdate);
			paramMap.put("enddate", enddate);
			paramMap.put("ismhtnosplit", ismhtnosplit);
			
			if (IConstDef.T1_LOAN_CREDIT.equals(operatetype)) {
				paramMap.put("credit_operation_type", IConstDef.T1_LOAN_CREDIT);
				paramMap.put("return_operation_type", IConstDef.T1_LOAN_RETURN);
			} else if (IConstDef.T0_LOAN_CREDIT.equals(operatetype)) {
				paramMap.put("credit_operation_type", IConstDef.T0_LOAN_CREDIT);
				paramMap.put("return_operation_type", IConstDef.T0_LOAN_RETURN);
			} else if (IConstDef.D1_LOAN_CREDIT.equals(operatetype)) {
				paramMap.put("credit_operation_type", IConstDef.D1_LOAN_CREDIT);
				paramMap.put("return_operation_type", IConstDef.D1_LOAN_RETURN);
			} else {
				paramMap.put("credit_operation_type", "");
				paramMap.put("return_operation_type", "");
			}
			
			if (page == null) {
				return getSqlSession().selectList(getSqlName(mapMethodName), paramMap);
			} else {
			return getSqlSession().selectList(getSqlName(mapMethodName), paramMap, 
					this.getPageBounds(page));
			}
		} catch (Exception e) {
			throw new DaoException(String.format("关键字：" + "operation_type->" + operatetype +", mhtno->" + mhtno +", startdate->" + startdate +", enddate->" + enddate +", ismhtnosplit->" + ismhtnosplit +", pageno->" + page.getPageNumber() + ", pagesize->" + page.getPageSize()  + "查询Tx贷款对账信息列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public List<T0LoanCheckAccountInfoVO> queryTotalTxLoanCheckAccountInfo(
			String operatetype, String mhtno, String startdate, String enddate, Pageable page) throws BusinessException {
		String mapMethodName = "queryTotalTxLoanCheckAccountInfo";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("mhtno", mhtno);
			paramMap.put("startdate", startdate);
			paramMap.put("enddate", enddate);
			
			if (IConstDef.T1_LOAN_CREDIT.equals(operatetype)) {
				paramMap.put("credit_operation_type", IConstDef.T1_LOAN_CREDIT);
				paramMap.put("return_operation_type", IConstDef.T1_LOAN_RETURN);
			} else if (IConstDef.T0_LOAN_CREDIT.equals(operatetype)) {
				paramMap.put("credit_operation_type", IConstDef.T0_LOAN_CREDIT);
				paramMap.put("return_operation_type", IConstDef.T0_LOAN_RETURN);
			} else if (IConstDef.D1_LOAN_CREDIT.equals(operatetype)) {
				paramMap.put("credit_operation_type", IConstDef.D1_LOAN_CREDIT);
				paramMap.put("return_operation_type", IConstDef.D1_LOAN_RETURN);
			} else {
				paramMap.put("credit_operation_type", "");
				paramMap.put("return_operation_type", "");
			}
			
			return getSqlSession().selectList(getSqlName(mapMethodName), paramMap, 
					this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("关键字：" + "operation_type->" + operatetype +", mhtno->" + mhtno +", startdate->" + startdate +", enddate->" + enddate +", pageno->" + page.getPageNumber() + ", pagesize->" + page.getPageSize()  + "查询T1贷款对账信息列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	
	@Override
	public TbT0LoanTransanctionLog queryCreditorInfoFromDebtorMhtNoAndDate(
			String debtor_mhtno, String creditdate) throws BusinessException {
		String mapMethodName = "queryCreditorInfoFromDebtorMhtNoAndDate";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("debtor_mhtno", debtor_mhtno);
			paramMap.put("creditdate", creditdate);
			
			List list = getSqlSession().selectList(getSqlName(mapMethodName),
					paramMap);
			if (list != null && list.size() >0)
				return (TbT0LoanTransanctionLog) list.get(0);
		} catch (Exception e) {
			throw new DaoException(String.format("查询指定日期指定商户的贷款业务债权用户信息出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		
		return null;
	}

	public List<T0LoanCheckAccountInfoVO> queryT0LoanBalanceInfoListByDebtorMhtno(String mhtno,Pageable page)
			throws BusinessException {
		String mapMethodName = "queryT0LoanBalanceInfoListByDebtorMhtno";
		try {
			if (page.getPageNumber() == IConstDef.PageAll && page.getPageSize() == IConstDef.PageAll) {
				return getSqlSession().selectList(getSqlName(mapMethodName),
						mhtno);
			} else {
				return getSqlSession().selectList(getSqlName(mapMethodName),
					mhtno,this.getPageBounds(page));
			}
		} catch (Exception e) {
			throw new DaoException(String.format("查询指定商户的实时T+0贷款负债信息列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		
	}

	public T0LoanCheckAccountInfoVO queryTxLoanBalanceInfoByDebtorMhtno(String mhtno)
			throws BusinessException {
		String mapMethodName = "queryTxLoanBalanceInfoByDebtorMhtno";
		try {
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("mhtno", mhtno);
			
			return getSqlSession().selectOne(getSqlName(mapMethodName),
					param);
		} catch (Exception e) {
			throw new DaoException(String.format("查询指定商户的实时T+1贷款负债信息出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public List<T0LoanTransanctionLogInfoVO> queryT0LoanTransanctionLogInfoListByDebtorMhtNo(Map param,Pageable pageable) throws BusinessException {
		String mapMethodName = "queryT0LoanTransanctionLogInfoListByDebtorMhtNo";
		try {
			List<T0LoanTransanctionLogInfoVO> contentList=getSqlSession().selectList(getSqlName(mapMethodName), param,getPageBounds(pageable));
			return contentList;
		} catch (Exception e) {
			throw new DaoException(String.format("查询指定债务人的贷款历史记录列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<T0LoanTransanctionLogInfoVO> queryTxLoanTransanctionLogInfoListByDebtorMhtNo(String debtor_mhtno, String operation_type, Pageable pageable) throws BusinessException {
		String mapMethodName = "queryTxLoanTransanctionLogInfoListByDebtorMhtNo";
		try {
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("debtor_mhtno", debtor_mhtno);
			param.put("operation_type", operation_type);
			
			List<T0LoanTransanctionLogInfoVO> contentList=getSqlSession().selectList(getSqlName(mapMethodName), param,getPageBounds(pageable));
			return contentList;
		} catch (Exception e) {
			throw new DaoException(String.format("查询指定债务人的Tx贷款历史记录列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public List<T0LoanTransanctionLogInfoVO> queryTxLoanTransanctionLogNotCreditInfoList(
			Pageable pageable, String operatetype) throws BusinessException {
		String mapMethodName = "queryTxLoanTransanctionLogNotCreditInfoList";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("operatetype", operatetype);
			List<T0LoanTransanctionLogInfoVO> contentList=getSqlSession().selectList(getSqlName(mapMethodName), paramMap, getPageBounds(pageable));
			return contentList;
		} catch (Exception e) {
			throw new DaoException(String.format("查询所有已审核未放款的贷款日志记录列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int updateT0LoanTransanctionLogCreditStatusByID(int id,
			int creditstatus, String creditAccountBalance) throws BusinessException {
		String mapMethodName = "updateT0LoanTransanctionLogCreditStatusByID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("id", id);
			paramMap.put("creditstatus", creditstatus);
			paramMap.put("creditAccountBalance", creditAccountBalance);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新放款日志状态出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
    @Override
    public void lockid(int id) throws BusinessException {
        String mapMethodName = "selectLockid";

        try {
            this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),
                     id);
        } catch (Exception e) {
            throw new DaoException(String.format("加锁T0LoanTransanctionLog出错, id->" + id + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public void lockTable() throws BusinessException {
        String mapMethodName = "locktable";

        try {
            this.sqlSessionTemplate.selectList(getSqlName(mapMethodName));
        } catch (Exception e) {
            throw new DaoException(String.format("加锁T0LoanTransanctionLog表出错->出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

	@Override
	public List<T0LoanTransanctionLogInfoVO> queryTxSettlementListByBusidate(String operatetype, String busidate) throws BusinessException {
		String mapMethodName = "queryTxSettlementListByBusidate";
		try {
			HashMap<String, Object> paramMap = new HashMap<>();
			paramMap.put("operatetype", operatetype);
			
			return getSqlSession().selectList(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("查询" + operatetype + "贷款结算单列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public T0LoanTransanctionLogInfoVO queryTxSettlementTotalByBusidate(String operatetype, String busidate) throws BusinessException {
		String mapMethodName = "queryTxSettlementTotalByBusidate";
		try {
			HashMap<String, Object> paramMap = new HashMap<>();
			paramMap.put("operatetype", operatetype);
			
			return getSqlSession().selectOne(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("查询" + operatetype + "贷款结算单汇总信息出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
    @Override
    public int updateT0LoanTransanctionLogVO(Map<String, Object> paramsmap) throws BusinessException {
        String mapMethodName ="updateT0LoanTransanctionLogVO";
        try{
            return this.sqlSessionTemplate.update(getSqlName(mapMethodName),paramsmap);
        }catch (Exception e) {
            throw new DaoException(String.format("更新T0贷款日志信息出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }
    
    /**
     * 查询指定商户未还清的贷款记录集合
     * @param joinuserid
     * @return
     * @throws BusinessException
     */
    public List<TbT0LoanTransanctionLog> queryUnRefundT0LoanTransanctionLogList(int joinuserid) throws BusinessException {
    	String mapMethodName ="queryUnRefundT0LoanTransanctionLogList";
        try{
        	HashMap<String, Object> paramMap = new HashMap<>();
			paramMap.put("joinuserid", joinuserid);
			
			return getSqlSession().selectList(getSqlName(mapMethodName), paramMap);
        }catch (Exception e) {
            throw new DaoException(String.format("查询指定商户未还清的贷款记录集合出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }
   
    /**
     * 查询未提现的贷款记录集合
     * @param joinuserid
     * @return
     * @throws BusinessException
     */
    public List<TbT0LoanTransanctionLog> queryUnDrawT0LoanTransanctionLogList(String operatetype, Pageable page) throws BusinessException {
    	String mapMethodName ="queryUnDrawT0LoanTransanctionLogList";
        try{
        	HashMap<String, Object> paramMap = new HashMap<>();
			paramMap.put("operatetype", operatetype);
			
			return getSqlSession().selectList(getSqlName(mapMethodName), paramMap, getPageBounds(page));
        }catch (Exception e) {
            throw new DaoException(String.format("查询未提现的贷款记录集合出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }
}
